package algotithm.leetcode.listnode.jianzhi06;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/19-20:58
 * @URL https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/
 * @Title 剑指 Offer 06. 从尾到头打印链表
 */

import algotithm.leetcode.listnode.ListNode;

import java.util.ArrayList;

/**
 * @author zhouyanxiang
 * @Date 2020-12-2020/12/10-14:07
 */

public class Solution {

    ArrayList<Integer> list = new ArrayList<>();

    // public static void main(String[] args) {
    //     ListNode node1 = new ListNode(10);
    //     ListNode node3 = new ListNode(30);
    //     ListNode node2 = new ListNode(20);
    //     node1.next = node3;
    //     node3.next = node2;
    //     Solution2 solution2 = new Solution2();
    //     int[] arr = solution2.reversePrint(node1);
    //     for (int num : arr) {
    //         System.out.print(num + " ");
    //     }
    // }

    /**
     *
     * @param head
     * @return
     */
    public int[] reversePrint(ListNode head) {
        int len = 0;
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = head;
        while (cur != null) {
            next = cur.next ;
            cur.next = pre;
            pre = cur;
            cur = next;
            len++;
        }
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            arr[i] = pre.val;
            pre = pre.next;
        }
        return arr;
    }

    // 递归解法
    /*public int[] reversePrint(ListNode head) {
        recur(head);
        int[] num = new int[list.size()];
        for (int i = 0; i < num.length; i++) {
            num[i] = list.get(i);
        }
        return num;
    }

    public void recur(ListNode head) {
        if (head == null) {
            return;
        }
        recur(head.next);
        list.add(head.val);
    }*/
}
